#pragma once

#include "Includes.h"
#include "Math/Array.h"

// Vertex format
struct TObj_Vertex
{
    float position[3];
    float normal[3];
    float texcoord[2];
};


// Used for a hashtable vertex cache when creating the mesh from a .obj file
struct TObj_CacheEntry
{
    mUInt index;
    TObj_CacheEntry* pNext;
};


// TObj_Material properties per mesh subset
struct TObj_Material
{
    mString strName;

    float vAmbient[3];
    float vDiffuse[3];
    float vSpecular[3];

    float fShininess;
    float fAlpha;

    mBool bSpecular;

    mString   strTexture;
};

class CObjLoader
{
public:
    CObjLoader            ( );
    ~CObjLoader           ( );

    mBool Create          ( mString strFilename );
    void  Destroy         ( );
    mUInt GetNumMaterials ( ) const
    {
        return m_Materials.GetSize();
    }

    mUInt GetNumSubsets   ( )
    {
        return m_NumAttribTableEntries;
    }

    TObj_Material* GetMaterial ( mUInt iMaterial = 0 )
    {
        return m_Materials.GetAt( iMaterial );
    }

    TObj_Vertex  *VertexPtr    ( )
    {
        return m_Vertices.GetData();
    }

    mUInt         VertexSize   ( )
    {
        return m_Vertices.GetSize();
    }

    mUInt32  *IndexPtr    ( )
    {
        return m_Indices.GetData();
    }

    mUInt         IndexSize   ( )
    {
        return m_Indices.GetSize();
    }

private:

    mBool LoadGeometryFromOBJ  ( mString strFilename );
    mBool LoadMaterialsFromMTL ( mString strFileName );
    mUInt AddVertex            ( mUInt hash, TObj_Vertex* pVertex );
    void  DeleteCache          ( );
    //void    InitMaterial         ( TObj_Material* pMaterial );

    CDynamicArray < TObj_CacheEntry* > m_VertexCache;   // Hashtable cache for locating duplicate vertices
    CDynamicArray < TObj_Vertex >      m_Vertices;      // Filled and copied to the vertex buffer
    CDynamicArray < mUInt32 >          m_Indices;       // Filled and copied to the index buffer
    CDynamicArray < mUInt >            m_Attributes;    // Filled and copied to the attribute buffer
    CDynamicArray < TObj_Material* >   m_Materials;     // Holds material properties per subset
    mUInt                              m_NumAttribTableEntries;
};